library(tidyverse) # for data cleaning and plotting
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3 ✓ purrr 0.3.4
## ✓ tibble 3.0.5 ✓ dplyr 1.0.3
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(lubridate) # for date manipulation
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(openintro) # for the abbr2state() function
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
library(palmerpenguins)# for Palmer penguin data
library(maps) # for map data
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
library(ggmap) # for mapping points on maps
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(gplots) # for col2hex() function
##
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
##
## lowess
library(RColorBrewer) # for color palettes
library(sf) # for working with spatial data
## Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1
library(leaflet) # for highly customizable mapping
library(carData) # for Minneapolis police stops data
library(ggthemes) # for more themes (including theme_map())
theme_set(theme_minimal())
# Starbucks locations
Starbucks <- read_csv("https://www.macalester.edu/~ajohns24/Data/Starbucks.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## Brand = col_character(),
## `Store Number` = col_character(),
## `Store Name` = col_character(),
## `Ownership Type` = col_character(),
## `Street Address` = col_character(),
## City = col_character(),
## `State/Province` = col_character(),
## Country = col_character(),
## Postcode = col_character(),
## `Phone Number` = col_character(),
## Timezone = col_character(),
## Longitude = col_double(),
## Latitude = col_double()
## )
starbucks_us_by_state <- Starbucks %>%
filter(Country == "US") %>%
count(`State/Province`) %>%
mutate(state_name = str_to_lower(abbr2state(`State/Province`)))
# Lisa's favorite St. Paul places - example for you to create your own data
favorite_stp_by_lisa <- tibble(
place = c("Home", "Macalester College", "Adams Spanish Immersion",
"Spirit Gymnastics", "Bama & Bapa", "Now Bikes",
"Dance Spectrum", "Pizza Luce", "Brunson's"),
long = c(-93.1405743, -93.1712321, -93.1451796,
-93.1650563, -93.1542883, -93.1696608,
-93.1393172, -93.1524256, -93.0753863),
lat = c(44.950576, 44.9378965, 44.9237914,
44.9654609, 44.9295072, 44.9436813,
44.9399922, 44.9468848, 44.9700727)
)
#COVID-19 data from the New York Times
covid19 <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## date = col_date(format = ""),
## state = col_character(),
## fips = col_character(),
## cases = col_double(),
## deaths = col_double()
## )
If you were not able to get set up on GitHub last week, go here and get set up first. Then, do the following (if you get stuck on a step, don’t worry, I will help! You can always get started on the homework and we can figure out the GitHub piece later):
keep_md: TRUE in the YAML heading. The .md file is a markdown (NOT R Markdown) file that is an interim step to creating the html file. They are displayed fairly nicely in GitHub, so we want to keep it and look at it there. Click the boxes next to these two files, commit changes (remember to include a commit message), and push them (green up arrow).Put your name at the top of the document.
For ALL graphs, you should include appropriate labels.
Feel free to change the default theme, which I currently have set to theme_minimal().
Use good coding practice. Read the short sections on good code with pipes and ggplot2. This is part of your grade!
When you are finished with ALL the exercises, uncomment the options at the top so your document looks nicer. Don’t do it before then, or else you might miss some important warnings and messages.
These exercises will reiterate what you learned in the “Mapping data with R” tutorial. If you haven’t gone through the tutorial yet, you should do that first.
ggmap)Starbucks locations to a world map. Add an aesthetic to the world map that sets the color of the points according to the ownership type. What, if anything, can you deduce from this visualization?I notice that certain ownership types are clustered in different geographies. For example, there are a lot of, if not all, joint ventures of the coast of Asia.
#Code from "Mapping data in R" tutorial
world <- get_stamenmap(
bbox = c(left = -180, bottom = -57, right = 179, top = 82.1),
maptype = "terrain",
zoom = 2)
## Source : http://tile.stamen.com/terrain/2/0/0.png
## Source : http://tile.stamen.com/terrain/2/1/0.png
## Source : http://tile.stamen.com/terrain/2/2/0.png
## Source : http://tile.stamen.com/terrain/2/3/0.png
## Source : http://tile.stamen.com/terrain/2/0/1.png
## Source : http://tile.stamen.com/terrain/2/1/1.png
## Source : http://tile.stamen.com/terrain/2/2/1.png
## Source : http://tile.stamen.com/terrain/2/3/1.png
## Source : http://tile.stamen.com/terrain/2/0/2.png
## Source : http://tile.stamen.com/terrain/2/1/2.png
## Source : http://tile.stamen.com/terrain/2/2/2.png
## Source : http://tile.stamen.com/terrain/2/3/2.png
ggmap(world) +
geom_point(data = Starbucks,
aes(x = Longitude, y = Latitude, color = `Ownership Type`),
alpha = .3,
size = .1) +
theme_map() +
labs(title = "Starbucks Across the World")
## Warning: Removed 1 rows containing missing values (geom_point).
tc <- get_stamenmap(
bbox = c(left = -95, bottom = 44.6543, right = -92.7811, top = 45.4514),
maptype = "terrain",
zoom = 11)
## 98 tiles needed, this may take a while (try a smaller zoom).
## Source : http://tile.stamen.com/terrain/11/483/733.png
## Source : http://tile.stamen.com/terrain/11/484/733.png
## Source : http://tile.stamen.com/terrain/11/485/733.png
## Source : http://tile.stamen.com/terrain/11/486/733.png
## Source : http://tile.stamen.com/terrain/11/487/733.png
## Source : http://tile.stamen.com/terrain/11/488/733.png
## Source : http://tile.stamen.com/terrain/11/489/733.png
## Source : http://tile.stamen.com/terrain/11/490/733.png
## Source : http://tile.stamen.com/terrain/11/491/733.png
## Source : http://tile.stamen.com/terrain/11/492/733.png
## Source : http://tile.stamen.com/terrain/11/493/733.png
## Source : http://tile.stamen.com/terrain/11/494/733.png
## Source : http://tile.stamen.com/terrain/11/495/733.png
## Source : http://tile.stamen.com/terrain/11/496/733.png
## Source : http://tile.stamen.com/terrain/11/483/734.png
## Source : http://tile.stamen.com/terrain/11/484/734.png
## Source : http://tile.stamen.com/terrain/11/485/734.png
## Source : http://tile.stamen.com/terrain/11/486/734.png
## Source : http://tile.stamen.com/terrain/11/487/734.png
## Source : http://tile.stamen.com/terrain/11/488/734.png
## Source : http://tile.stamen.com/terrain/11/489/734.png
## Source : http://tile.stamen.com/terrain/11/490/734.png
## Source : http://tile.stamen.com/terrain/11/491/734.png
## Source : http://tile.stamen.com/terrain/11/492/734.png
## Source : http://tile.stamen.com/terrain/11/493/734.png
## Source : http://tile.stamen.com/terrain/11/494/734.png
## Source : http://tile.stamen.com/terrain/11/495/734.png
## Source : http://tile.stamen.com/terrain/11/496/734.png
## Source : http://tile.stamen.com/terrain/11/483/735.png
## Source : http://tile.stamen.com/terrain/11/484/735.png
## Source : http://tile.stamen.com/terrain/11/485/735.png
## Source : http://tile.stamen.com/terrain/11/486/735.png
## Source : http://tile.stamen.com/terrain/11/487/735.png
## Source : http://tile.stamen.com/terrain/11/488/735.png
## Source : http://tile.stamen.com/terrain/11/489/735.png
## Source : http://tile.stamen.com/terrain/11/490/735.png
## Source : http://tile.stamen.com/terrain/11/491/735.png
## Source : http://tile.stamen.com/terrain/11/492/735.png
## Source : http://tile.stamen.com/terrain/11/493/735.png
## Source : http://tile.stamen.com/terrain/11/494/735.png
## Source : http://tile.stamen.com/terrain/11/495/735.png
## Source : http://tile.stamen.com/terrain/11/496/735.png
## Source : http://tile.stamen.com/terrain/11/483/736.png
## Source : http://tile.stamen.com/terrain/11/484/736.png
## Source : http://tile.stamen.com/terrain/11/485/736.png
## Source : http://tile.stamen.com/terrain/11/486/736.png
## Source : http://tile.stamen.com/terrain/11/487/736.png
## Source : http://tile.stamen.com/terrain/11/488/736.png
## Source : http://tile.stamen.com/terrain/11/489/736.png
## Source : http://tile.stamen.com/terrain/11/490/736.png
## Source : http://tile.stamen.com/terrain/11/491/736.png
## Source : http://tile.stamen.com/terrain/11/492/736.png
## Source : http://tile.stamen.com/terrain/11/493/736.png
## Source : http://tile.stamen.com/terrain/11/494/736.png
## Source : http://tile.stamen.com/terrain/11/495/736.png
## Source : http://tile.stamen.com/terrain/11/496/736.png
## Source : http://tile.stamen.com/terrain/11/483/737.png
## Source : http://tile.stamen.com/terrain/11/484/737.png
## Source : http://tile.stamen.com/terrain/11/485/737.png
## Source : http://tile.stamen.com/terrain/11/486/737.png
## Source : http://tile.stamen.com/terrain/11/487/737.png
## Source : http://tile.stamen.com/terrain/11/488/737.png
## Source : http://tile.stamen.com/terrain/11/489/737.png
## Source : http://tile.stamen.com/terrain/11/490/737.png
## Source : http://tile.stamen.com/terrain/11/491/737.png
## Source : http://tile.stamen.com/terrain/11/492/737.png
## Source : http://tile.stamen.com/terrain/11/493/737.png
## Source : http://tile.stamen.com/terrain/11/494/737.png
## Source : http://tile.stamen.com/terrain/11/495/737.png
## Source : http://tile.stamen.com/terrain/11/496/737.png
## Source : http://tile.stamen.com/terrain/11/483/738.png
## Source : http://tile.stamen.com/terrain/11/484/738.png
## Source : http://tile.stamen.com/terrain/11/485/738.png
## Source : http://tile.stamen.com/terrain/11/486/738.png
## Source : http://tile.stamen.com/terrain/11/487/738.png
## Source : http://tile.stamen.com/terrain/11/488/738.png
## Source : http://tile.stamen.com/terrain/11/489/738.png
## Source : http://tile.stamen.com/terrain/11/490/738.png
## Source : http://tile.stamen.com/terrain/11/491/738.png
## Source : http://tile.stamen.com/terrain/11/492/738.png
## Source : http://tile.stamen.com/terrain/11/493/738.png
## Source : http://tile.stamen.com/terrain/11/494/738.png
## Source : http://tile.stamen.com/terrain/11/495/738.png
## Source : http://tile.stamen.com/terrain/11/496/738.png
## Source : http://tile.stamen.com/terrain/11/483/739.png
## Source : http://tile.stamen.com/terrain/11/484/739.png
## Source : http://tile.stamen.com/terrain/11/485/739.png
## Source : http://tile.stamen.com/terrain/11/486/739.png
## Source : http://tile.stamen.com/terrain/11/487/739.png
## Source : http://tile.stamen.com/terrain/11/488/739.png
## Source : http://tile.stamen.com/terrain/11/489/739.png
## Source : http://tile.stamen.com/terrain/11/490/739.png
## Source : http://tile.stamen.com/terrain/11/491/739.png
## Source : http://tile.stamen.com/terrain/11/492/739.png
## Source : http://tile.stamen.com/terrain/11/493/739.png
## Source : http://tile.stamen.com/terrain/11/494/739.png
## Source : http://tile.stamen.com/terrain/11/495/739.png
## Source : http://tile.stamen.com/terrain/11/496/739.png
ggmap(tc) +
geom_point(data = Starbucks,
aes(x = Longitude, y = Latitude, color = `Ownership Type`),
size = .3) +
theme_map() +
labs(title = "Map of Twin Cities Starbucks")
## Warning: Removed 25447 rows containing missing values (geom_point).
If we have a low zoom number like the world map, it is really blurry and you can’t make out the map. Increasing zoom also adds more highways and streets. It also decreases the size of the city labels and adds more of them.
get_stamenmap() in help and look at maptype). Include a map with one of the other map types.world_toner <- get_stamenmap(
bbox = c(left = -180, bottom = -57, right = 179, top = 82.1),
maptype = "toner",
zoom = 2)
## Source : http://tile.stamen.com/toner/2/0/0.png
## Source : http://tile.stamen.com/toner/2/1/0.png
## Source : http://tile.stamen.com/toner/2/2/0.png
## Source : http://tile.stamen.com/toner/2/3/0.png
## Source : http://tile.stamen.com/toner/2/0/1.png
## Source : http://tile.stamen.com/toner/2/1/1.png
## Source : http://tile.stamen.com/toner/2/2/1.png
## Source : http://tile.stamen.com/toner/2/3/1.png
## Source : http://tile.stamen.com/toner/2/0/2.png
## Source : http://tile.stamen.com/toner/2/1/2.png
## Source : http://tile.stamen.com/toner/2/2/2.png
## Source : http://tile.stamen.com/toner/2/3/2.png
ggmap(world_toner) +
labs(title ="World Map") +
theme_map()
annotate() function (see ggplot2 cheatsheet).ggmap(world_toner) +
geom_point() +
annotate(geom = "point", x = - 93.1691, y =44.937, color = "red") +
annotate(geom = "text", x = - 93.1691, y =49, label = "Macalester", color = "red")+
labs(title = "World Map with Macalester College") +
theme_map()
geom_map())The example I showed in the tutorial did not account for population of each state in the map. In the code below, a new variable is created, starbucks_per_10000, that gives the number of Starbucks per 10,000 people. It is in the starbucks_with_2018_pop_est dataset.
census_pop_est_2018 <- read_csv("https://www.dropbox.com/s/6txwv3b4ng7pepe/us_census_2018_state_pop_est.csv?dl=1") %>%
separate(state, into = c("dot","state"), extra = "merge") %>%
select(-dot) %>%
mutate(state = str_to_lower(state))
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## state = col_character(),
## est_pop_2018 = col_double()
## )
starbucks_with_2018_pop_est <-
starbucks_us_by_state %>%
left_join(census_pop_est_2018,
by = c("state_name" = "state")) %>%
mutate(starbucks_per_10000 = (n/est_pop_2018)*10000)
dplyr review: Look through the code above and describe what each line of code does.The first line follows the link and reads the CSV file into a data frame. The second line separates the state variable into two variables so we are able to get rid of the “.” in the state variable in the next line. The mutate function changes the states to be in all lowercase.
The Starbucks data first names the new data set and then merges it with the census data and matches the “state_name” and “state” variables. The last line creates a new variables for the number of Starbucks per 10000 people in that state.
There are more Starbucks per capita the further northeast you travel which makes sense since that is where they started the company.
states_map <- map_data("state")
# map that colors state by number of Starbucks
starbucks_with_2018_pop_est %>%
ggplot() +
geom_map(map = states_map,
aes(map_id = state_name,
fill = starbucks_per_10000)) +
expand_limits(x = states_map$long, y = states_map$lat) +
theme_map() +
scale_fill_viridis_c(option = "B") +
labs(title = "Starbucks per 10,000 People",
caption = "Kennedy Kechely")
leaflet)tibble() function that has 10-15 rows of your favorite places. The columns will be the name of the location, the latitude, the longitude, and a column that indicates if it is in your top 3 favorite locations or not. For an example of how to use tibble(), look at the favorite_stp_by_lisa I created in the data R code chunk at the beginning.favorite_stp_by_kennedy <- tibble(
place = c("Rochester Home", "Macalester Softball Field", "Target",
"School Home", "Lake Bde Maka Ska", "Moka Coffee",
"Dunkin' Donuts", "Butler University", "Lincoln, NE",
"Breckenridge, CO"),
long = c(-92.492270, -93.169980, -93.155610,
-93.169320, -93.306450, -92.463114,
-93.16700744628906, -86.16897583007812, -96.7077751,
-106.04692840576172),
lat = c(44.013100, 44.934770, 44.953250,
44.944720, 44.948140, 44.0334518,
44.9447135925293, 39.83837127685547, 40.8088861,
39.473114013671875),
top3 = c("Not top 3","Top 3","Not top 3",
"Not top 3","Not top 3","Top 3",
"Not top 3","Not top 3","Not top 3",
"Top 3")
)
leaflet map that uses circles to indicate your favorite places. Label them with the name of the place. Choose the base map you like best. Color your 3 favorite places differently than the ones that are not in your top 3 (HINT: colorFactor()). Add a legend that explains what the colors mean.pal <- colorFactor("viridis",
domain = favorite_stp_by_kennedy$top3)
leaflet(data = favorite_stp_by_kennedy) %>%
addProviderTiles(providers$Stamen.TonerBackground) %>%
addCircles(lng = ~long,
lat = ~lat,
label = ~place,
weight = 10,
opacity = 1,
color = ~pal(top3)) %>%
addLegend(pal = pal,
values= ~top3,
position = "bottomright",
title = NULL)
leaflet(data = favorite_stp_by_kennedy) %>%
addProviderTiles(providers$Stamen.TonerBackground) %>%
addCircles(lng = ~long,
lat = ~lat,
label = ~place,
weight = 10,
opacity = 1,
color = ~pal(top3)) %>%
addLegend(pal = pal,
values= ~top3,
position = "bottomright",
title = NULL,
labels = ~c("Not top 3", "Top 3")) %>%
addPolylines(lng = ~long,
lat = ~lat,
color = col2hex("darkred"))
This section will revisit some datasets we have used previously and bring in a mapping component.
The data come from Washington, DC and cover the last quarter of 2014.
Two data tables are available:
Trips contains records of individual rentalsStations gives the locations of the bike rental stationsHere is the code to read in the data. We do this a little differently than usualy, which is why it is included here rather than at the top of this file. To avoid repeatedly re-reading the files, start the data import chunk with {r cache = TRUE} rather than the usual {r}. This code reads in the large dataset right away.
data_site <-
"https://www.macalester.edu/~dshuman1/data/112/2014-Q4-Trips-History-Data.rds"
Trips <- readRDS(gzcon(url(data_site)))
Stations<-read_csv("http://www.macalester.edu/~dshuman1/data/112/DC-Stations.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## name = col_character(),
## lat = col_double(),
## long = col_double(),
## nbBikes = col_double(),
## nbEmptyDocks = col_double()
## )
Stations to make a visualization of the total number of departures from each station in the Trips data. Use either color or size to show the variation in number of departures. This time, plot the points on top of a map. Use any of the mapping tools you’d like.TripStationData <- Trips %>%
group_by(sstation) %>%
summarize(departures = n(),
name = sstation) %>%
left_join(Stations,
by = c("name")) %>%
distinct(sstation, .keep_all = TRUE)
## `summarise()` has grouped output by 'sstation'. You can override using the `.groups` argument.
WashDC <- get_stamenmap(
bbox = c(left = -77.3891, bottom = 38.7128, right = -76.6406, top = 39.0746),
maptype = "toner-background",
zoom = 10)
## Source : http://tile.stamen.com/toner-background/10/291/391.png
## Source : http://tile.stamen.com/toner-background/10/292/391.png
## Source : http://tile.stamen.com/toner-background/10/293/391.png
## Source : http://tile.stamen.com/toner-background/10/294/391.png
## Source : http://tile.stamen.com/toner-background/10/291/392.png
## Source : http://tile.stamen.com/toner-background/10/292/392.png
## Source : http://tile.stamen.com/toner-background/10/293/392.png
## Source : http://tile.stamen.com/toner-background/10/294/392.png
ggmap(WashDC) +
geom_point(data = TripStationData,
aes(x = long, y = lat, size = TripStationData$departures, alpha = .1)
) +
labs(title = "Washington DC Density of Train Departures")
## Warning: Removed 33 rows containing missing values (geom_point).
theme_map()
## List of 93
## $ line :List of 6
## ..$ colour : chr "black"
## ..$ size : num 0.409
## ..$ linetype : num 1
## ..$ lineend : chr "butt"
## ..$ arrow : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_line" "element"
## $ rect :List of 5
## ..$ fill : chr "white"
## ..$ colour : chr "black"
## ..$ size : num 0.409
## ..$ linetype : num 1
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_rect" "element"
## $ text :List of 11
## ..$ family : chr ""
## ..$ face : chr "plain"
## ..$ colour : chr "black"
## ..$ size : num 9
## ..$ hjust : num 0.5
## ..$ vjust : num 0.5
## ..$ angle : num 0
## ..$ lineheight : num 0.9
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ title : NULL
## $ aspect.ratio : NULL
## $ axis.title : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.title.x :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 2.25points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.x.top :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 2.25points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.x.bottom : NULL
## $ axis.title.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : num 90
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 2.25points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.y.left : NULL
## $ axis.title.y.right :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : num -90
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 2.25points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.text.x :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 1.8points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x.top :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 1.8points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x.bottom : NULL
## $ axis.text.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 1
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 1.8points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.y.left : NULL
## $ axis.text.y.right :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 1.8points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.ticks : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.ticks.x : NULL
## $ axis.ticks.x.top : NULL
## $ axis.ticks.x.bottom : NULL
## $ axis.ticks.y : NULL
## $ axis.ticks.y.left : NULL
## $ axis.ticks.y.right : NULL
## $ axis.ticks.length : 'simpleUnit' num 2.25points
## ..- attr(*, "unit")= int 8
## $ axis.ticks.length.x : NULL
## $ axis.ticks.length.x.top : NULL
## $ axis.ticks.length.x.bottom: NULL
## $ axis.ticks.length.y : NULL
## $ axis.ticks.length.y.left : NULL
## $ axis.ticks.length.y.right : NULL
## $ axis.line : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.line.x : NULL
## $ axis.line.x.top : NULL
## $ axis.line.x.bottom : NULL
## $ axis.line.y : NULL
## $ axis.line.y.left : NULL
## $ axis.line.y.right : NULL
## $ legend.background :List of 5
## ..$ fill : NULL
## ..$ colour : logi NA
## ..$ size : NULL
## ..$ linetype : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_rect" "element"
## $ legend.margin : 'margin' num [1:4] 4.5points 4.5points 4.5points 4.5points
## ..- attr(*, "unit")= int 8
## $ legend.spacing : 'simpleUnit' num 9points
## ..- attr(*, "unit")= int 8
## $ legend.spacing.x : NULL
## $ legend.spacing.y : NULL
## $ legend.key :List of 5
## ..$ fill : chr "white"
## ..$ colour : logi NA
## ..$ size : NULL
## ..$ linetype : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_rect" "element"
## $ legend.key.size : 'simpleUnit' num 1.2lines
## ..- attr(*, "unit")= int 3
## $ legend.key.height : NULL
## $ legend.key.width : NULL
## $ legend.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ legend.text.align : NULL
## $ legend.title :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ legend.title.align : NULL
## $ legend.position : num [1:2] 0 0
## $ legend.direction : NULL
## $ legend.justification : num [1:2] 0 0
## $ legend.box : NULL
## $ legend.box.just : NULL
## $ legend.box.margin : 'margin' num [1:4] 0cm 0cm 0cm 0cm
## ..- attr(*, "unit")= int 1
## $ legend.box.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.box.spacing : 'simpleUnit' num 9points
## ..- attr(*, "unit")= int 8
## $ panel.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ panel.border : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ panel.spacing : 'simpleUnit' num 0lines
## ..- attr(*, "unit")= int 3
## $ panel.spacing.x : NULL
## $ panel.spacing.y : NULL
## $ panel.grid : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ panel.grid.major : NULL
## $ panel.grid.minor :List of 6
## ..$ colour : NULL
## ..$ size : 'rel' num 0.5
## ..$ linetype : NULL
## ..$ lineend : NULL
## ..$ arrow : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_line" "element"
## $ panel.grid.major.x : NULL
## $ panel.grid.major.y : NULL
## $ panel.grid.minor.x : NULL
## $ panel.grid.minor.y : NULL
## $ panel.ontop : logi FALSE
## $ plot.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ plot.title :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 1.2
## ..$ hjust : num 0
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 4.5points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.title.position : chr "panel"
## $ plot.subtitle :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 4.5points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.caption :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 0.8
## ..$ hjust : num 1
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 4.5points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.caption.position : chr "panel"
## $ plot.tag :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 1.2
## ..$ hjust : num 0.5
## ..$ vjust : num 0.5
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.tag.position : chr "topleft"
## $ plot.margin : 'margin' num [1:4] 4.5points 4.5points 4.5points 4.5points
## ..- attr(*, "unit")= int 8
## $ strip.background :List of 5
## ..$ fill : chr "grey85"
## ..$ colour : chr "grey20"
## ..$ size : NULL
## ..$ linetype : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_rect" "element"
## $ strip.background.x : NULL
## $ strip.background.y : NULL
## $ strip.placement : chr "inside"
## $ strip.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : chr "grey10"
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 3.6points 3.6points 3.6points 3.6points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ strip.text.x : NULL
## $ strip.text.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : num -90
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ strip.switch.pad.grid : 'simpleUnit' num 2.25points
## ..- attr(*, "unit")= int 8
## $ strip.switch.pad.wrap : 'simpleUnit' num 2.25points
## ..- attr(*, "unit")= int 8
## $ strip.text.y.left :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : num 90
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## - attr(*, "class")= chr [1:2] "theme" "gg"
## - attr(*, "complete")= logi TRUE
## - attr(*, "validate")= logi TRUE
The majority of the casual riders are right in the center near some of the tourist attractions in DC.
CasData <- Trips %>%
mutate(casual = ifelse(client == "Casual",1, 0)) %>%
group_by(sstation) %>%
summarize(departures = n(),
name = sstation,
total_cas = sum(casual),
percent_cas = total_cas/departures * 100) %>%
left_join(Stations,
by = c("name")) %>%
distinct(sstation, .keep_all = TRUE)
## `summarise()` has grouped output by 'sstation'. You can override using the `.groups` argument.
ggmap(WashDC) +
geom_point(data=CasData,
aes(x = long, y = lat, color = percent_cas)) +
labs(title = "Washington DC Train Departures",
x = "",
y = "")
## Warning: Removed 33 rows containing missing values (geom_point).
The following exercises will use the COVID-19 data from the NYT.
We don’t have any population scale.
covid19 %>%
group_by(state) %>%
summarize(recentcount = max(cases)) %>%
mutate(state = str_to_lower(state)) %>%
ggplot() +
geom_map(map = states_map,
aes(map_id = state,
fill = recentcount)) +
expand_limits(x = states_map$long, y = states_map$lat) +
theme_map() +
labs(title = "Cumulative Number of COVID 19 Cases")
covid_with_2018_pop_est <-
covid19 %>%
group_by(state) %>%
summarize(recentcount = max(cases)) %>%
mutate(state = str_to_lower(state)) %>%
left_join(census_pop_est_2018,
by = c("state" = "state")) %>%
mutate(cases_per_10000 = (recentcount/est_pop_2018)*10000)
ggplot(data = covid_with_2018_pop_est) +
geom_map(map = states_map,
aes(map_id = state,
fill = cases_per_10000)) +
expand_limits(x = states_map$long, y = states_map$lat) +
theme_map() +
labs(title = "Cumulative Number of COVID 19 Cases per 10,000 People")
I tried to facet_wrap by date but failed. I think the rest of the code is right though.
# covid19 %>%
# filter(date == "2020-03-01" |
# date == "2020-05-01"|
# date == "2020-08-01"|
# date == "2020-11-01") %>%
# mutate(state = str_to_lower(state)) %>%
# left_join(census_pop_est_2018,
# by = c("state" = "state")) %>%
# mutate(cases_per_10000 = (cases/est_pop_2018)*10000) %>%
#
# ggplot() +
# facet_wrap(date) +
# geom_map(map = states_map,
# aes(map_id = state,
# fill = cases_per_10000)) +
# expand_limits(x = states_map$long, y = states_map$lat) +
# theme_map() +
# labs(title = "Cumulative Number of COVID 19 Cases per 10,000 People")
These exercises use the datasets MplsStops and MplsDemo from the carData library. Search for them in Help to find out more information.
MplsStops dataset to find out how many stops there were for each neighborhood and the proportion of stops that were for a suspicious vehicle or person. Sort the results from most to least number of stops. Save this as a dataset called mpls_suspicious and display the table.mpls_suspicious <- MplsStops %>%
group_by(neighborhood) %>%
summarize(stops_per_neighborhood = n(),
sus_stops_prop = sum(problem=="suspicious")/stops_per_neighborhood) %>%
arrange(desc(stops_per_neighborhood))
mpls_suspicious
leaflet map and the MplsStops dataset to display each of the stops on a map as a small point. Color the points differently depending on whether they were for suspicious vehicle/person or a traffic stop (the problem variable). HINTS: use addCircleMarkers, set stroke = FAlSE, use colorFactor() to create a palette.pal_traffic <- colorFactor("viridis",
domain = MplsStops$problem)
leaflet(data = MplsStops) %>%
addProviderTiles(providers$Stamen.TonerBackground) %>%
addCircleMarkers(lng = ~long,
lat = ~lat,
weight = 5,
opacity = 0.5,
color = ~pal_traffic(problem),
stroke= FALSE) %>%
addLegend(pal = pal_traffic,
values= ~problem,
position = "bottomright",
title = NULL)
eval=FALSE. Although it looks like it only links to the .sph file, you need the entire folder of files to create the mpls_nbhd data set. These data contain information about the geometries of the Minneapolis neighborhoods. Using the mpls_nbhd dataset as the base file, join the mpls_suspicious and MplsDemo datasets to it by neighborhood (careful, they are named different things in the different files). Call this new dataset mpls_all.mpls_nbhd <- st_read("Minneapolis_Neighborhoods/Minneapolis_Neighborhoods.shp", quiet = TRUE)
mpls_all <- mpls_nbhd %>%
left_join(mpls_suspicious,
by = c("BDNAME" = "neighborhood")) %>%
left_join(MplsDemo,
by = c("BDNAME" = "neighborhood"))
leaflet to create a map from the mpls_all data that colors the neighborhoods by prop_suspicious. Display the neighborhood name as you scroll over it. Describe what you observe in the map.There are a relatively high proportion of traffic stops that are suspicious in the southeast part and a relatively low proportion in the northeast. The green areas are more in the middle of the distribution.
pal17 <- colorNumeric("viridis",
domain = mpls_all$sus_stops_prop)
leaflet(mpls_all) %>%
addTiles() %>%
addPolygons(
fillColor = ~pal17(sus_stops_prop),
fillOpacity = 0.7,
label = ~BDNAME) %>%
addLegend(pal = pal17,
values = ~sus_stops_prop)
leaflet to create a map of your own choosing. Come up with a question you want to try to answer and use the map to help answer that question. Describe what your map shows.This map shows the household income in each neighborhood. The wealthier neighborhoods are in the southwest of Minneapolis and the poorer neighborhoods are in the center of the city.
pal18 <- colorNumeric("magma",
domain = mpls_all$hhIncome)
leaflet(mpls_all) %>%
addTiles() %>%
addPolygons(
fillColor = ~pal18(hhIncome),
fillOpacity = 0.7,
label = ~BDNAME) %>%
addLegend(pal = pal18,
values = ~hhIncome)
DID YOU REMEMBER TO UNCOMMENT THE OPTIONS AT THE TOP?